package com.wys.controller;

import com.wys.pojo.ShoppingCart;
import com.wys.result.R;
import com.wys.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        return shoppingCartService.add(shoppingCart,session);
    }

    /**
     *  查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getShoppingCart(HttpSession session){

        return R.success(shoppingCartService.getShoppingCart(session));
    }

    /**
     *  清空购物车
     * @return
     */
    @DeleteMapping("clean")
    public R<String> deletedShoppingCart(HttpSession session){
        shoppingCartService.deletedShoppingCart(session);
        return R.success("成功清空购物车");
    }

    /**
     * 购物车中菜品数量减一
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<String> deletedWithOne(@RequestBody ShoppingCart shoppingCart,HttpSession session){

        return shoppingCartService.deletedWithOne(shoppingCart,session);
    }
}
